use std::f32::consts::PI;

use ggez::{glam::Vec2, Context, graphics, mint::Point2};



pub struct Pendulum {
    location: Vec2,
    origin: Vec2,
    r: f32,
    angle: f32,
    a_velocity: f32,
    a_acceleration: f32,
    damping: f32
}

impl Pendulum {
    pub fn new(origin: Vec2, r: f32) -> Pendulum {
        let location = Vec2{x: 0.0, y: 0.0};
        let angle = PI/4.0;
        let a_velocity = 0.0;
        let a_acceleration = 0.0;
        let damping = 0.995;
        Pendulum { location, origin, r, angle, a_velocity, a_acceleration, damping}
    }

    pub fn update(&mut self) -> () {
        let gravity = 0.4;
        self.a_acceleration = (-1.0*gravity/self.r)*(self.angle.sin());
        self.a_velocity += self.a_acceleration;
        self.angle += self.a_velocity;
        self.a_velocity *= self.damping;
        self.location.x = self.r*self.angle.sin();
        self.location.y = self.r*self.angle.cos();
    }

    pub fn display(&self, ctx: &mut Context, canvas : &mut graphics::Canvas) -> () {
        let param = graphics::DrawParam::default().dest(self.origin);
        let line = graphics::Mesh::new_line(
            ctx, 
            &[Point2{x: 0.0, y: 0.0}, Point2{x: self.location.x, y: self.location.y}], 
            2.0, 
            graphics::Color::BLACK
        ).unwrap();
        canvas.draw(&line, param);
        
        let circle = graphics::Mesh::new_circle(
            ctx, 
            graphics::DrawMode::stroke(2.0), 
            self.location, 
            20.0, 
            0.1, 
            graphics::Color::BLACK).unwrap();
        canvas.draw(&circle, param);

        let circle = graphics::Mesh::new_circle(
            ctx, 
            graphics::DrawMode::fill(), 
            self.location, 
            20.0, 
            0.1, 
            graphics::Color::new(0.7, 0.7, 0.7, 1.0)).unwrap();
        canvas.draw(&circle, param); 

    }
}